#include "AddQuestionFrame.h"

//(*InternalHeaders(AddQuestionFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)
#include "sqlite3.h"


//(*IdInit(AddQuestionFrame)
const long AddQuestionFrame::ID_STATICTEXT1 = wxNewId();
const long AddQuestionFrame::ID_TEXTCTRL1 = wxNewId();
const long AddQuestionFrame::ID_BUTTON2 = wxNewId();
const long AddQuestionFrame::ID_BUTTON1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(AddQuestionFrame,wxFrame)
	//(*EventTable(AddQuestionFrame)
	//*)
END_EVENT_TABLE()

AddQuestionFrame::AddQuestionFrame(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(AddQuestionFrame)
	wxBoxSizer* BoxSizer2;
	wxBoxSizer* BoxSizer1;
	wxBoxSizer* BoxSizer3;

	Create(parent, wxID_ANY, _("添加试题"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
	BoxSizer1 = new wxBoxSizer(wxVERTICAL);
	BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
	StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("试题"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
	BoxSizer2->Add(StaticText1, 0, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	BoxSizer2->Add(TextCtrl1, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	BoxSizer1->Add(BoxSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
	Button2 = new wxButton(this, ID_BUTTON2, _("完工"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
	BoxSizer3->Add(Button2, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	Button1 = new wxButton(this, ID_BUTTON1, _("再说"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
	BoxSizer3->Add(Button1, 1, wxALL|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	BoxSizer1->Add(BoxSizer3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(BoxSizer1);
	BoxSizer1->Fit(this);
	BoxSizer1->SetSizeHints(this);

	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&AddQuestionFrame::OnCommitQuestionButtonClick);
	//*)
}

AddQuestionFrame::~AddQuestionFrame()
{
	//(*Destroy(AddQuestionFrame)
	//*)
}


void AddQuestionFrame::OnCommitQuestionButtonClick(wxCommandEvent& event)
{
    //获取试题，并构造sql语句；
    wxString MemoContent;
    MemoContent = TextCtrl1->GetValue();

    //连接数据库
    sqlite3 *ChemistryDB = NULL;
    wxString DataBaseName = _T("chemistryDB.db");
    wxString DataBasePath;
    DataBasePath = GetExePath() + DataBaseName;
    int rc;
    rc = sqlite3_open(DataBasePath.mb_str(wxConvUTF8), &ChemistryDB);
    if (rc)
    {
      wxString msg = _T("Failed");
      wxMessageBox(msg, _("Create DataBase failed"));
      sqlite3_close(ChemistryDB);
    }

//    char* CreateTableSql = "create table TestTB(id integer primary key,"
//                           "                    num integer,"
//                           "                    context text)";
//    rc = sqlite3_exec(ChemistryDB, CreateTableSql, NULL, NULL, NULL);

    wxString InsertTableSql = _T("insert into ChemistryTest(KeyWord) values (\"") + MemoContent + _T("\")");
    rc = sqlite3_exec(ChemistryDB, InsertTableSql.mb_str(wxConvUTF8), NULL, NULL, NULL);

    wxString InsertBlobSql = _T("insert into ChemistryTest(Question) values (?)");
    sqlite3_stmt *stmt = NULL;

    rc = sqlite3_prepare(ChemistryDB, InsertBlobSql.mb_str(wxConvUTF8), -1, &stmt, 0);
    void *buffer;
    //buffer = new char
//
//    char* InsertTableSq2 = "insert into TestTB(num, context) values (2, 'come on baby')";
//    rc = sqlite3_exec(ChemistryDB, InsertTableSq2, NULL, NULL, NULL);

//    char* QueryTableSql = "select * from TestTB";
//    char *CallBackData = "call back has been called";
//    rc = sqlite3_exec(ChemistryDB, QueryTableSql, DBExecCallBack, CallBackData, NULL);

    sqlite3_close(ChemistryDB);
}
